import time
import sys
import MyUtil
import cls
import TradeSt
import tdxblock
import multiprocessing
import get_hq_Worker_FeedTest
import os
import GUI_NB
import pandas as pd
import const
import selectionFunBack
import TradeApi
import math
import feedL2Data
import feedL2Data_sina
import back_feed_his
try:
    import redis
except Exception as e:
    print(e)
STOCKS_INST_DIC = {}
BLOCKS_INST_DIC = {}
SELECT_SYM_BLK = []
symbols = [] # list of  tuple  (nMarket, sStockCode)



feed_mode=True
zt_feed_mode = True
reset_debug=True

L2_HQ_DEBUG = False

# HQ_DATA_FILE_TEST ='D:\\FBYX\\get_hq_x\\bigdata\HQ_DATA\\20180814hq - 副本.db' # 测试指定行情db文件
symbol_test = '603706'
# back_test_day = '20180814'

hq_data_folder = '' # 'D:\\tmp_creat_db' #'E:\\hq_data'#'F:\\TDX_2018行情数据'  #'D:\\tmp_creat_db' #'E:\\hq_data'  #
zhubi_folder = 'D:\\zhubi'
SIMULATE = True  # 模拟
FEED_TICK_NUM = 500
download_flg= False      # 下载历历史行情数据  和制作历史数据
make_his_data_flg = False    #制作历史数据,如果存在则不需要另外做了,这个可以默认为true,重做历史数据只需要把对应日期文件夹删除
OPEN_GUI = False
DATA_SOURCE_SINA = False     # 运行2017年数据新浪的,

TX_ONE_MIN_FLG = True  #单独挑选分钟量比的tick# 不进行策略,只挑选满足分钟量比的数据
NO_REDIS_MODE = True  # 不经过redis 模式
all_tick_flag = False   # 是提取500tick还是提取所有tick
auction_back_test = True  # 只对集合竞价进行回测
###################################################自动对历史数据库行情进行回测#########################################

STOCK_ONE_MIN_HIS = []
ASK_BID_HIS = {}

def main(auto_online_gethis):
    #
    if OPEN_GUI:
        (con1, con2) = multiprocessing.Pipe()  # conn1只能用来接收消息,conn2只能用来发送消息
        p = multiprocessing.Process(target=GUI_NB.DisplayGUI, args=(con1,auto_online_gethis,))
        p.start()
    #
    # download_flg = False
    # download_is = con2.recv()
    # if download_is=='download_yes':
    #     download_flg=True

    # ----------------------------------------------------
    #day_line_dic = back_feed_his.get_day_line_dic()

    hq_data_lst = os.listdir(hq_data_folder)
    back_test_day_lst = []
    if not DATA_SOURCE_SINA:
        back_test_day_lst = [item.split('hq')[0] for item in hq_data_lst]
    else:
        for item in hq_data_lst:
            if item.split('hq')[0] not in back_test_day_lst:  # 20170616hq_13.db
                back_test_day_lst.append(item.split('hq')[0])
        print('back_test_day_lst:',back_test_day_lst, 'len:', len(back_test_day_lst))
    back_day_idx = 0
    back_test_day = back_test_day_lst[back_day_idx]
    HQ_DATA_FILE_TEST = hq_data_folder + os.sep + hq_data_lst[back_day_idx]
    
    print('file number:',len(back_test_day_lst))

    # --------------------------------getHisDataOnLine-------------------------------------------------------------------
    if download_flg==True:
        print('在线获取历史数据....')
        starttime = time.time()
        back_feed_his.getHisDataOnLine()    #
        endtime = time.time()
        print(u'获取历史数据耗时s:', round((endtime - starttime), 0))

    # -----------------------------------StockCodeList------------------------------------------------------------------
    symbols = MyUtil.getAllStockList()  # list of  tuple  (nMarket, sStockCode)
    if symbols == [] or symbols == None:
        print('symbols=', symbols)
        return

    # --------------------------------makeStockHisInfo------------------------------------------------------------------
    if make_his_data_flg == True:
        print('离线制作个股历史数据信息....')
        # csv_data_dic = {}
        # finance_csv_dic = {}
        process_num = 6
        i = 0
        #   ---------------------# 根据历史数据文件夹 ,已经有历史数据的不进行分组,
        his_data_folder_lst = os.listdir(const.STOCK_HIS_BACK_INFO)
        back_test_day_lst_tmp = []
        back_test_day_lst_mk = [item for item in back_test_day_lst]
        for item in back_test_day_lst_mk:
            if item not in his_data_folder_lst:
                back_test_day_lst_tmp.append(item)
        back_test_day_lst_mk = back_test_day_lst_tmp
        print('make his data:',back_test_day_lst_mk)
        proc_pool = {}  # 记录创建的所有子进程
        cntl_q = multiprocessing.Queue()  # 控制信息传递队列
        # ---------------------
        if len(back_test_day_lst_mk) > 0:
            step = math.ceil(len(back_test_day_lst_mk) / process_num)
            back_test_day_batch = [back_test_day_lst_mk[i:i + step] for i in range(0, len(back_test_day_lst_mk), step)]
            for item in back_test_day_batch:
                p1 = multiprocessing.Process(target=back_feed_his.makeStockHisInfo_batch,
                                             args=([item[1] for item in symbols], item,cntl_q,))
                p1.start()
                proc_pool[p1.pid] = p1
                print('process id:', item)
            while True:
                time.sleep(10)
                try:
                    item_event = cntl_q.get()
                    if item_event['event'] == 'exit':
                        proc = proc_pool.pop(item_event['pid'])
                        proc.join()
                        print('finish makeStockHisInfo_batch PID:',item_event['pid'])
                except Exception as e:
                    MyUtil.print_error(e)
            his_data_folder_lst = os.listdir(const.STOCK_HIS_BACK_INFO)
        # for item_back_day in back_test_day_lst:
        #     back_feed_his.makeStockHisInfo([item[1] for item in symbols], item_back_day, csv_data_dic, finance_csv_dic)  #
        #     print('finished', item_back_day)

    # 根据历史数据文件夹 ,对没有历史数据的股票数据库不进行提取
    his_data_folder_lst = os.listdir(const.STOCK_HIS_BACK_INFO)
    back_test_day_lst_tmp = []
    for item in back_test_day_lst:
        if item in his_data_folder_lst:
            back_test_day_lst_tmp.append(item)
    back_test_day_lst = back_test_day_lst_tmp
    back_day_idx = 0
    back_test_day = back_test_day_lst[back_day_idx]
    print('back_test_day:',back_test_day)

    # --------------------------------get block info-------------------------------------------------------------------
    # print('解析本地文件获得板块数据信息....')
    # tdxblock.makeBlocksInfo(symbols)   #制作板块信息，包括解析板块成分股，和获取板块指数代码
    hy2_info = tdxblock.getBlockCode('hy2') # {880301:['T0101', '煤炭', 'hy2'],...}
    gn_info = tdxblock.getBlockCode('gn')
                                                ###getBlockChildres()  #参考tdxblock模块main函数
    blockofstock = tdxblock.getBlockOfStockDic()
    day_line_dic = {}
    back_feed_his.create_stock_inst(symbols, STOCKS_INST_DIC, blockofstock, back_test_day , day_line_dic)


    # ----------------------------------------make db--------------------------------------------------------------------
    # stock_codes = [item[1] for item in symbols]
    # msqliteObj = sqliteCls.sqliteObj()
    # msqliteObj.create_stock_table(stock_codes)
    # msqliteObj._cursor.close()
    # msqliteObj.conn.close()
    # msqliteObj = None
    # ----------------------------------------start write---------------------------------------------------------------
    # if not feed_mode:
    #     mreceiveWorker = ReceiveWorker.receiveWorker(symbols)
    #     mreceiveWorker.start()

    # --------------------------------create all block instance--------------------------------------------------------
    for block_symbol in gn_info:
        newblockcls = cls.tdx_block_cls()
        newblockcls.symbol = block_symbol
        newblockcls.name = gn_info[block_symbol][1]
        newblockcls.mtype = 'gn'
        newblockcls.children = [item[0] for item in tdxblock.getBlockChildres(gn_info[block_symbol][0],'gn')]
        BLOCKS_INST_DIC[block_symbol] = newblockcls
    for block_symbol in hy2_info:
        newblockcls = cls.tdx_block_cls()
        newblockcls.symbol = block_symbol
        newblockcls.name = hy2_info[block_symbol][1]
        newblockcls.mtype = 'hy'
        newblockcls.children = [item[0] for item in tdxblock.getBlockChildres(hy2_info[block_symbol][0], 'hy2')]
        BLOCKS_INST_DIC[block_symbol] = newblockcls
    ## -----------------------------设置板块5日历史数据-------------------------------------------------------------
    Allblocks = list(BLOCKS_INST_DIC.keys())
    # if not feed_mode:
    #     HisData.GetBlock5DayHq(Allblocks,BLOCKS_INST_DIC)
    #     time.sleep(3)
    # --------------------------------getHqData-----------------------------------------------------------------------
    # print('启动行情线程获取数据')
    # #Allblocks = list(hy2_info.keys())+list(gn_info.keys())
    # if L2_HQ_DEBUG:
    #     L2hqWorker = get_hq_Worker_FeedTest.L2hqWorker(symbols)
    #     L2hqWorker.start()
    #     hqWorker = get_hq_Worker.hqWorker(Allblocks)
    #     hqWorker.start()

    mgrSel_cls = cls.mgrSel()
    market = cls.markert()
    # --------------------------------receiver-----------------------------------------------------------------------
    try:
        try:
            # __redis__ = redis.StrictRedis(decode_responses=True, host="127.0.0.1", port=6379)
            # __redis__.client_list()
            # __mlistener__ = __redis__.pubsub()  # Redis Pubsub 命令用于查看订阅与发布系统状态，它由数个不同格式的子命令组成
            # __mlistener__.subscribe([const.L2_HQ_CHANNEL,const.BLOCK_HQ_CHANEL])  ##写入数据库监听器1
            redis_cls = cls.redis_cls()
            __redis__ = redis_cls.__redis__
            __mlistener__ = redis_cls.__mlistener__
            __mlistener__.subscribe([const.L2_HQ_CHANNEL_BACK])
        except redis.ConnectionError:
            print("Cannot connect to redis")

    except redis.ConnectionError:
        print("Cannot connect to redis")

    mTRADEAPI = back_feed_his.ModulePickle()
    # mTRADEAPI.login()
    lasttime = time.time()
    if feed_mode and not NO_REDIS_MODE:
        print('启动feed data')
        if not DATA_SOURCE_SINA:
            p1 = multiprocessing.Process(target=feedL2Data.main, args=(HQ_DATA_FILE_TEST,SIMULATE,symbol_test,FEED_TICK_NUM,))
            p1.start()
        else:
            p1 = multiprocessing.Process(target=feedL2Data_sina.main,
                                         args=(hq_data_folder, back_test_day, SIMULATE, symbol_test, FEED_TICK_NUM,))
            p1.start()
    no_redis_cls = None
    if NO_REDIS_MODE:
        print('!!!!!!!!!!!!!!!!!no redis mode !!!!!!')
        no_redis_cls = feedL2Data.no_redis_main(HQ_DATA_FILE_TEST, SIMULATE, symbol_test, FEED_TICK_NUM)
        detail_tran = back_feed_his.DetailTran(zhubi_folder, back_test_day)
    #------------------------------------------get configration---------------------------------------------------------
    config_desp_list, config_trade_list = selectionFunBack.getConfigration()
    config_tradeSt_list = TradeSt.getConfigration()
    time_l2_idx = time.time()
    time_block_idx = time_l2_idx
    time_rank_idx = time_l2_idx
    time_selection_idx = time_l2_idx
    time_send_idx = time_l2_idx
    time_query_idx = time_l2_idx
    # time_zt_shoot = time_l2_idx
    tick_i = 0
    tick_bath = math.ceil(len(symbols) / 100)
    while True and not NO_REDIS_MODE:
        stock_lst = []
        # time.sleep(0.001)                 !!!!!!!!  拷贝main代码到此,请记住把交易注释掉  !!!!!!!!!!!!!!!!!!!!!
        period_block = 0
        curr_time_idx = time.time()
        local_time = time.localtime(curr_time_idx)
        mhour = local_time.tm_hour
        mminu = local_time.tm_min
        parsed_msg = __mlistener__.get_message()
        if parsed_msg is not None and parsed_msg != [] \
                and (parsed_msg['channel'] == const.L2_HQ_CHANNEL_BACK):
            data = parsed_msg['data']
            try:
                if data != None and type(data) == str:
                    data_in = eval(data)
                    # --------------------------------------L2_HQ_CHANNEL------------------------------------------
                    if parsed_msg['channel'] == const.L2_HQ_CHANNEL_BACK:
                        tick_i += 1
                        time_l2_idx = time.time()
                        # print('data_in[0]', data_in[0])
                        for item in data_in:
                            try:
                                STOCKS_INST_DIC[item['symbol']].setL2HqData(item)
                                selectionFunBack.CalcL2Hq(item['symbol'], STOCKS_INST_DIC, mgrSel_cls)  # 单个股票单独计算
                                stock_lst.append(item['symbol'])
                            except Exception as e:
                                MyUtil.print_error(e)
                                # print('L2 time:', round(1000 * (time.time() - time_l2_idx), 2))
                    # --------------------------------------BLOCK_HQ_CHANEL---------------------------------------------
                    # if parsed_msg['channel'] == const.BLOCK_HQ_CHANEL:
                    #     period_block = 1000 * (curr_time_idx - time_block_idx)
                    #     time_block_idx = time.time()
                    #     try:
                    #         for item in data_in:
                    #             try:
                    #                 BLOCKS_INST_DIC[item['code']].setHqData(item)
                    #             except Exception as e:
                    #                 pass
                    #                 # print('BLOCK time:', round(1000 * (time.time() - time_block_idx), 2))
                    #     except Exception as e:
                    #         MyUtil.print_error(e)
                    # --------------------------------------RANK---------------------------------------------
                    if tick_i % (tick_bath*2) == 0:
                        try:
                            time_rank_idx = time.time()
                            selectionFunBack.CalcHq(BLOCKS_INST_DIC, STOCKS_INST_DIC, mgrSel_cls)  # 全部一起计算
                            selectionFunBack.calc_broad(STOCKS_INST_DIC, market)
                            #print('rank calc time:', round(1000 * (time.time() - time_rank_idx), 2))
                        except Exception as e:
                            pass
                    # --------------------------------------ZT_SHOOT---------------------------------------------
                    # if curr_time_idx - time_zt_shoot >= 3:
                    #     time_zt_shoot = time.time()
                    #     ZtShoot.online_select(STOCKS_INST_DIC, zt_pool_cls)
                    # try:
                    #     if __redis__ and len(zt_pool_cls.zt_shoot_pool) > 0:
                    #         __redis__.publish(const.HIGH_LST_CHANEL, zt_pool_cls.zt_shoot_pool)
                    # except Exception as e:
                    #     MyUtil.print_error(e)
                    # --------------------------------------SELECT---------------------------------------------
                    if True:
                        #MyUtil.prn_obj(STOCKS_INST_DIC[feedL2Data.symbol_test])
                        print('tick_i:', round(tick_i/tick_bath))
                        try:
                            time_selection_idx = time.time()
                            if TX_ONE_MIN_FLG:
                                back_feed_his.selection(mgrSel_cls, STOCKS_INST_DIC, BLOCKS_INST_DIC, stock_lst)
                            # ====================================================保留==============================
                            else:
                                buy_flg = selectionFunBack.selection(mgrSel_cls, STOCKS_INST_DIC, BLOCKS_INST_DIC,
                                                                 config_desp_list,
                                                                 config_trade_list,
                                                                 time_selection_idx)  # print('启动选择.....')
                                if buy_flg :
                                    back_feed_his.TRADE_ST(mgrSel_cls, mTRADEAPI, config_tradeSt_list, None,time_selection_idx)
                                    print('selection time:', round(1000 * (time.time() - time_selection_idx), 2))
                            # ====================================================保留==============================
                        except Exception as e:
                            MyUtil.print_error(e)
                    if len(mgrSel_cls.SLECETION_LIST)>=const.DISPLY_SELECT_MAX:
                        back_feed_his.updataSelectionStock(None, mgrSel_cls.SLECETION_LIST, STOCKS_INST_DIC,
                                                           back_test_day)
                        mgrSel_cls.SLECETION_LIST=[]

                    if int(tick_i/tick_bath) >= FEED_TICK_NUM-1:        # finished one db
                        __redis__.publish(feedL2Data.STOP_SEND_CHANNEL, '1')
                        print('==============================>finished ====================>:',back_test_day)
                        time.sleep(1)
                        try:
                            back_feed_his.updataSelectionStock(None, mgrSel_cls.SLECETION_LIST, STOCKS_INST_DIC,
                                                               back_test_day)
                            print('完成tick:',int(tick_i/tick_bath))
                            mgrSel_cls.SLECETION_LIST = []
                            if not os.path.exists(const.SELECT_TICKS_BACK + os.sep + back_test_day):
                                os.makedirs(const.SELECT_TICKS_BACK + os.sep + back_test_day)
                            back_feed_his.merger_csv(const.SELECT_TICKS_BACK + os.sep + back_test_day,
                                                     const.SELECT_TICKS_BACK + os.sep + back_test_day, back_test_day)
                            save_name = 'backtest_' + back_test_day + '.csv'
                            save_folder = const.SELECT_TICKS_BACK + os.sep + back_test_day
                            print('merge csv finished!!')
                            merge_file = save_folder + os.sep+ save_name
                            save_file = save_folder + os.sep + 'backtest_' + back_test_day+'_profit' + '.csv'
                            if TX_ONE_MIN_FLG:
                                save_file = const.SELECT_TICKS_BACK + \
                                            os.sep + 'backtest_' + back_test_day+'_profit' + '.csv'
                            back_feed_his.calc_profit(merge_file, save_file, day_line_dic)
                            print('calc_profit finished')
                            if not TX_ONE_MIN_FLG:
                                # 挑出买入的股票放入csv中
                                save_buy_tick_csv_path = const.SELECT_TICKS_BACK + os.sep + 'sender.csv'
                                save_all_tick_csv_path = const.SELECT_TICKS_BACK + os.sep + 'all_ticks.csv'
                                df = pd.read_csv(save_file, encoding='gbk')

                                if not os.path.exists(save_all_tick_csv_path):
                                    df.to_csv(save_all_tick_csv_path, index=False, encoding="gbk")
                                else:
                                    df.to_csv(save_all_tick_csv_path, encoding="gbk",  header=False, index=False, mode='a+')

                                df[['send_order']] = df[['send_order']].astype(int)
                                df=df[df['send_order'] > 0]
                                if not os.path.exists(save_buy_tick_csv_path):
                                    df.to_csv(save_buy_tick_csv_path, index=False, encoding="gbk")
                                else:
                                    df.to_csv(save_buy_tick_csv_path, encoding="gbk",  header=False, index=False, mode='a+')

                        except Exception as e:
                            MyUtil.print_error(e)
                        #
                        try:
                            p1.terminate()
                            p1.join()
                            time.sleep(1)
                            try:
                                # keys = __redis__.keys()
                                # __redis__.delete(*keys)
                                pass
                            except Exception as e:
                                MyUtil.print_error(e)
                            redis_cls = None
                            redis_cls = cls.redis_cls()
                            __redis__ = redis_cls.__redis__
                            __mlistener__ = redis_cls.__mlistener__
                            __mlistener__.subscribe([const.L2_HQ_CHANNEL_BACK])
                            tick_i = 0
                            back_day_idx += 1       # update index

                            
                            print('back_day_idx:',back_day_idx,'len(back_test_day_lst):',len(back_test_day_lst))

                            if back_day_idx >= len(back_test_day_lst) :
                                print('finished!!!!!!!!!!!!!!!!!!!!!!')
                                break
                            back_test_day = back_test_day_lst[back_day_idx]

                            HQ_DATA_FILE_TEST = hq_data_folder + os.sep + hq_data_lst[back_day_idx]

                            mgrSel_cls = None
                            mgrSel_cls = cls.mgrSel()
                            market = None
                            market = cls.markert()
                            mTRADEAPI = None
                            mTRADEAPI = back_feed_his.ModulePickle()

                            day_line_dic.clear()
                            back_feed_his.create_stock_inst(symbols, STOCKS_INST_DIC, blockofstock, back_test_day,
                                                            day_line_dic)
                            if not DATA_SOURCE_SINA:
                                p1 = multiprocessing.Process(target=feedL2Data.main,
                                                             args=(HQ_DATA_FILE_TEST, SIMULATE, symbol_test, FEED_TICK_NUM,))
                                p1.start()
                            else:
                                p1 = multiprocessing.Process(target=feedL2Data_sina.main,
                                                             args=(hq_data_folder, back_test_day, SIMULATE, symbol_test, FEED_TICK_NUM,))
                                p1.start()
                        except Exception as e:
                            MyUtil.print_error(e)


                    # -------------------------------------- SEND GUI---------------------------------------------
                    # if curr_time_idx - time_send_idx >= 30 or len(mgrSel_cls.SLECETION_LIST) > const.DISPLY_SELECT_MAX:
                    #
                    #     time_send_idx = time.time()
                    #     con2.send('000111')
                    #     con2.send(STOCKS_INST_DIC)
                    #     con2.send(BLOCKS_INST_DIC)
                    #     con2.send(mgrSel_cls.SLECETION_LIST)
                    #     if len(mgrSel_cls.SLECETION_LIST) > const.DISPLY_SELECT_MAX:
                    #         mgrSel_cls.SLECETION_LIST = []
                    #     mgrDic = {}
                    #     mgrDic[const.L2_HQ_DIFF] = 9000
                    #     mgrDic[const.BLOCK_HQ_DIFF] = round(period_block, 0)
                    #     con2.send(mgrDic)
                    #     print('send time:', round(1000 * (time.time() - time_send_idx), 2))
                    # -------------------------------------- QUERY ---------------------------------------------
                    # if curr_time_idx - time_query_idx >= 180 and False:
                    #     time_query_idx = time.time()
                    #     # query_time = time.localtime(query_time_idx)
                    #     # endd =time.time()
                    #     # print('====',1000*(endd-statt))
                    #     # 0 资金
                    #     # 1 股份
                    #     # 2 当日委托
                    #     # 3 当日成交
                    #     # 4 可撤单
                    #     Money_TRD = mTRADEAPI.QueryDataAPI(0)
                    #     Stocks_TRD = mTRADEAPI.QueryDataAPI(1)
                    #     order_tdy_TRD = mTRADEAPI.QueryDataAPI(2)
                    #     turnover_TRD = mTRADEAPI.QueryDataAPI(3)
                    #     cancel_TRD = mTRADEAPI.QueryDataAPI(4)
                    #     con2.send(const.PIP_TRATDE_INFO)
                    #     con2.send(Money_TRD)
                    #     con2.send(Stocks_TRD)
                    #     con2.send(order_tdy_TRD)
                    #     con2.send(turnover_TRD)
                    #     con2.send(cancel_TRD)
                    #     print('query time:', round(1000 * (time.time() - time_query_idx), 2))
                        # ==============================================

            except Exception as e:
                print(e)
        if time.time()-curr_time_idx>0.5:
            print('run_time more than 0.5s')

        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------
        #  ------------------------------------------------------------------------------------------------

    while NO_REDIS_MODE:
        if no_redis_cls:
            data_in, finish_flg = no_redis_cls.get_tick()
            if not data_in and (not finish_flg):
                continue
            for item in data_in:
                try:
                    STOCKS_INST_DIC[item['symbol']].setL2HqData(item)
                    selectionFunBack.CalcL2Hq(item['symbol'], STOCKS_INST_DIC, mgrSel_cls)  # 单个股票单独计算
                except Exception as e:
                    MyUtil.print_error(e)
            # rank
            try:
                selectionFunBack.CalcHq(BLOCKS_INST_DIC, STOCKS_INST_DIC, mgrSel_cls)  # 全部一起计算
                selectionFunBack.calc_broad(STOCKS_INST_DIC, market)
            except Exception as e:
                pass
            # save one min his
            # back_feed_his.save_one_min_his(STOCKS_INST_DIC, STOCK_ONE_MIN_HIS, back_test_day)
            # select and buy
            # back_feed_his.calc_ask_bid_10tick(STOCKS_INST_DIC, ASK_BID_HIS)

            if TX_ONE_MIN_FLG:
                if not auction_back_test:
                    back_feed_his.selection(mgrSel_cls, STOCKS_INST_DIC, BLOCKS_INST_DIC, None, market,
                                            STOCK_ONE_MIN_HIS,ASK_BID_HIS,detail_tran)
                if auction_back_test:
                    back_feed_his.selection_auction(mgrSel_cls, STOCKS_INST_DIC, BLOCKS_INST_DIC, None, market,
                                                    STOCK_ONE_MIN_HIS, ASK_BID_HIS, detail_tran)
            else:
                buy_flg = selectionFunBack.selection(mgrSel_cls, STOCKS_INST_DIC, BLOCKS_INST_DIC,
                                                 config_desp_list,
                                                 config_trade_list,
                                                 time_selection_idx, market)  # print('启动选择.....')
                if buy_flg:
                    back_feed_his.TRADE_ST(mgrSel_cls, mTRADEAPI, config_tradeSt_list, None, time_selection_idx)

            if len(mgrSel_cls.SLECETION_LIST) >= const.DISPLY_SELECT_MAX:
                back_feed_his.updataSelectionStock(None, mgrSel_cls.SLECETION_LIST, STOCKS_INST_DIC,
                                                   back_test_day)
                mgrSel_cls.SLECETION_LIST = []
            finish_flg_final = False
            if not all_tick_flag:
                if no_redis_cls.tick_i >= FEED_TICK_NUM - 1:
                    finish_flg_final = True
            else:
                if finish_flg:
                    finish_flg_final = True
            if finish_flg_final:  # finished one db
                print('==============================>finished ====================>:', back_test_day)
                try:
                    back_feed_his.updataSelectionStock(None, mgrSel_cls.SLECETION_LIST, STOCKS_INST_DIC,
                                                       back_test_day)

                    mgrSel_cls.SLECETION_LIST = []
                    if not os.path.exists(const.SELECT_TICKS_BACK + os.sep + back_test_day):
                        os.makedirs(const.SELECT_TICKS_BACK + os.sep + back_test_day)
                    back_feed_his.merger_csv(const.SELECT_TICKS_BACK + os.sep + back_test_day,
                                             const.SELECT_TICKS_BACK + os.sep + back_test_day, back_test_day)
                    save_name = 'backtest_' + back_test_day + '.csv'
                    save_folder = const.SELECT_TICKS_BACK + os.sep + back_test_day
                    print('merge csv finished!!')
                    merge_file = save_folder + os.sep + save_name
                    save_file = save_folder + os.sep + 'backtest_' + back_test_day + '_profit' + '.csv'
                    if TX_ONE_MIN_FLG:
                        save_file = const.SELECT_TICKS_BACK + \
                                    os.sep + 'backtest_' + back_test_day + '_profit' + '.csv'
                    back_feed_his.calc_profit(merge_file, save_file, day_line_dic)
                    print('calc_profit finished')
                    if not TX_ONE_MIN_FLG:
                        # 挑出买入的股票放入csv中
                        save_buy_tick_csv_path = const.SELECT_TICKS_BACK + os.sep + 'sender.csv'
                        save_all_tick_csv_path = const.SELECT_TICKS_BACK + os.sep + 'all_ticks.csv'
                        df = pd.read_csv(save_file, encoding='gbk')

                        if not os.path.exists(save_all_tick_csv_path):
                            df.to_csv(save_all_tick_csv_path, index=False, encoding="gbk")
                        else:
                            df.to_csv(save_all_tick_csv_path, encoding="gbk", header=False, index=False, mode='a+')

                        df[['send_order']] = df[['send_order']].astype(int)
                        df = df[df['send_order'] > 0]
                        if not os.path.exists(save_buy_tick_csv_path):
                            df.to_csv(save_buy_tick_csv_path, index=False, encoding="gbk")
                        else:
                            df.to_csv(save_buy_tick_csv_path, encoding="gbk", header=False, index=False, mode='a+')

                except Exception as e:
                    MyUtil.print_error(e)
                #
                try:

                    redis_cls = None

                    back_day_idx += 1  # update index

                    print('back_day_idx:', back_day_idx, 'len(back_test_day_lst):', len(back_test_day_lst))

                    if back_day_idx >= len(back_test_day_lst):
                        print('finished!!!!!!!!!!!!!!!!!!!!!!')
                        break
                    back_test_day = back_test_day_lst[back_day_idx]

                    HQ_DATA_FILE_TEST = hq_data_folder + os.sep + hq_data_lst[back_day_idx]

                    no_redis_cls = None
                    no_redis_cls = feedL2Data.no_redis_main(HQ_DATA_FILE_TEST, SIMULATE, symbol_test,
                                                            FEED_TICK_NUM,all_tick_flag)
                    detail_tran = None
                    detail_tran = back_feed_his.DetailTran(zhubi_folder, back_test_day)

                    mgrSel_cls = None
                    mgrSel_cls = cls.mgrSel()
                    market = None
                    market = cls.markert()
                    mTRADEAPI = None
                    mTRADEAPI = back_feed_his.ModulePickle()

                    day_line_dic.clear()
                    back_feed_his.create_stock_inst(symbols, STOCKS_INST_DIC, blockofstock, back_test_day,
                                                    day_line_dic)

                except Exception as e:
                    MyUtil.print_error(e)





if __name__=='__main__':
    auto_online_gethis=False
    if len(sys.argv) == 2:
        auto_online_gethis=True
    main(auto_online_gethis)
    back_feed_his.merge_sele(TX_ONE_MIN_FLG)

